package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.DecimalUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.app.dto.MerchatOrderInitDataDTO;
import org.springblade.modules.app.dto.OrderTjDTO;
import org.springblade.modules.app.entity.*;
import org.springblade.modules.app.service.*;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 活动类型 表 控制器
 *
 * @author BladeX
 * @since 2020-08-24
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/ordertj")
@Api(value = "活动类型 表", tags = "活动类型 表接口")
public class OrderTjController extends BladeController {

    private final ICoalService coalService;
    private final IOrderDetailService orderDetailService;
    private final IUserService userService;

    private final ICouponsConfigService couponsConfigService;

    private final ICouponsService couponsService;

    private final IHelpstarService helpstarService;

    private final IMoneyDetailService moneyDetailService;

    ////订单初始化
    @GetMapping("/merchatOrderInitData")
    public R merchatOrderInitData(Helpstar helpstar) {
        BladeUser bladeUser = AuthUtil.getUser();
        User merchantUserInfo = userService.getById(bladeUser.getUserId());

        MerchatOrderInitDataDTO result = new MerchatOrderInitDataDTO();
        result.setProncecode(merchantUserInfo.getProncecode());
        result.setProncename(merchantUserInfo.getProncename());
        result.setCitycode(merchantUserInfo.getCitycode());
        result.setCityname(merchantUserInfo.getCityname());
        result.setAreacode(merchantUserInfo.getAreacode());
        result.setAreaname(merchantUserInfo.getAreaname());
        result.setAddress(merchantUserInfo.getAddress());
        result.setPcadinfo(merchantUserInfo.getProncename() + "/" + merchantUserInfo.getCityname() + "/" + merchantUserInfo.getAreaname() + "/" + merchantUserInfo.getAddress());
        result.setCuserphone(merchantUserInfo.getPhone());


        LambdaQueryWrapper<Coupons> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq(Coupons::getDelflag, "1"));
        queryWrapper.and(wrapper -> wrapper.like(Coupons::getUserid, bladeUser.getUserId()));
        queryWrapper.orderByDesc(Coupons::getDiscountprice);
        List<Coupons> couponsList = couponsService.list(queryWrapper);

        if (!couponsList.isEmpty()) {
            Coupons coupons = couponsList.get(0);
            result.setDiscountid(coupons.getId());
            result.setDiscountprice(coupons.getDiscountprice());
        } else {
            result.setDiscountid(0L);
            result.setDiscountprice(new BigDecimal(0.00));
        }

        if (!StringUtils.isEmpty(helpstar.getId())) {
            Helpstar product = helpstarService.getById(helpstar.getId());
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(null);
            orderDetail.setProductid(product.getId() + "");
            orderDetail.setPname(product.getPname());
            orderDetail.setPcount(1);
            orderDetail.setPmoney(product.getPmoney());
            orderDetail.setTotmoney(product.getPmoney());

            List<OrderDetail> orderDetailList = new ArrayList<>();

            orderDetailList.add(orderDetail);

            List<Helpstar> listAll = helpstarService.list(new LambdaQueryWrapper<Helpstar>().ne(Helpstar::getId, helpstar.getId()));
            for (Helpstar entity : listAll) {
                OrderDetail single = new OrderDetail();
                single.setId(null);
                single.setProductid(entity.getId() + "");
                single.setPname(entity.getPname());
                single.setPcount(0);
                single.setPmoney(entity.getPmoney());
                single.setTotmoney(new BigDecimal(0.00));
                orderDetailList.add(single);
            }
            result.setOrderDetailList(orderDetailList);
        } else {
            List<OrderDetail> orderDetailList = new ArrayList<>();
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(null);
            orderDetail.setProductid("");
            orderDetail.setPname("");
            orderDetail.setPcount(0);
            orderDetail.setPmoney(new BigDecimal(0.00));
            orderDetail.setTotmoney(new BigDecimal(0.00));
            orderDetailList.add(orderDetail);
            result.setOrderDetailList(orderDetailList);
        }

        return R.data(result);
    }


    ////今天的订单 今天的收益
    @GetMapping("/workderOrderTjToday")
    public R workderOrderTjToday(Order coal) {
        BladeUser bladeUser = AuthUtil.getUser();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        QueryWrapper<Order> queryWrappercm = new QueryWrapper<>();
        queryWrappercm.eq("workerid", bladeUser.getUserId());
        queryWrappercm.eq("orderstatus", "4");
        queryWrappercm.apply("date_format(ctime,'%Y-%m-%d')={0}", sdf.format(new Date()));
        int complateCount = coalService.count(queryWrappercm);


        QueryWrapper<Order> queryWrappercn = new QueryWrapper<>();
        queryWrappercn.eq("workerid", bladeUser.getUserId());
        queryWrappercn.eq("orderstatus", "5");
        queryWrappercn.apply("date_format(ctime,'%Y-%m-%d')={0}", sdf.format(new Date()));
        int cancleCount = coalService.count(queryWrappercn);

        LambdaQueryWrapper<MoneyDetail> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.and(wrapper -> wrapper.eq(MoneyDetail::getSkfuserid, bladeUser.getUserId()));
        lambdaQueryWrapper.and(wrapper -> wrapper.eq(MoneyDetail::getMoneytype, "1"));
        List<MoneyDetail> list = moneyDetailService.list(lambdaQueryWrapper);
        BigDecimal sumMoney = new BigDecimal("0.00");
        for (MoneyDetail entity : list) {
            sumMoney = DecimalUtil.add(sumMoney, entity.getTrademoney());
        }
        OrderTjDTO result = new OrderTjDTO();
        result.setComplatecount(complateCount);
        result.setCanclecount(cancleCount);
        result.setMakemoney(sumMoney.toString());

        return R.data(result);
    }

    ////月份订单
    @GetMapping("/workderOrderTjMonth")
    public R workderOrderTjMonth(Order coal) {
        BladeUser bladeUser = AuthUtil.getUser();
        List<OrderTjDTO> result = new ArrayList<>();
        OrderTjDTO by = getThisMonth(bladeUser);
        OrderTjDTO sy = getBeforOneMonth(bladeUser);
        OrderTjDTO qy = getBeforTwoMonth(bladeUser);
        result.add(by);
        result.add(sy);
        result.add(qy);

        return R.data(result);
    }


    public OrderTjDTO getThisMonth(BladeUser bladeUser) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        QueryWrapper<Order> queryWrappercm = new QueryWrapper<>();
        queryWrappercm.eq("workerid", bladeUser.getUserId());
        queryWrappercm.eq("orderstatus", "4");
        queryWrappercm.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(new Date()));
        int complateCount = coalService.count(queryWrappercm);
        QueryWrapper<Order> queryWrappercn = new QueryWrapper<>();
        queryWrappercn.eq("workerid", bladeUser.getUserId());
        queryWrappercn.eq("orderstatus", "5");
        queryWrappercn.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(new Date()));
        int cancleCount = coalService.count(queryWrappercn);

        QueryWrapper<MoneyDetail> queryWrappermk = new QueryWrapper<>();
        queryWrappermk.eq("skfuserid", bladeUser.getUserId());
        queryWrappermk.eq("moneytype", "1");
        queryWrappermk.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(new Date()));
        List<MoneyDetail> list = moneyDetailService.list(queryWrappermk);
        BigDecimal sumMoney = new BigDecimal("0.00");
        for (MoneyDetail entity : list) {
            sumMoney = DecimalUtil.add(sumMoney, entity.getTrademoney());
        }
        OrderTjDTO result = new OrderTjDTO();
        result.setComplatecount(complateCount);
        result.setCanclecount(cancleCount);
        result.setMakemoney(sumMoney.toString());
        result.setTjtitle(sdf.format(new Date()));
        return result;
    }

    private OrderTjDTO getBeforOneMonth(BladeUser bladeUser) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        Date date = cal.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        QueryWrapper<Order> queryWrappercm = new QueryWrapper<>();
        queryWrappercm.eq("workerid", bladeUser.getUserId());
        queryWrappercm.eq("orderstatus", "4");
        queryWrappercm.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        int complateCount = coalService.count(queryWrappercm);

        QueryWrapper<Order> queryWrappercn = new QueryWrapper<>();
        queryWrappercn.eq("workerid", bladeUser.getUserId());
        queryWrappercn.eq("orderstatus", "5");
        queryWrappercn.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        int cancleCount = coalService.count(queryWrappercn);


        QueryWrapper<MoneyDetail> queryWrappermk = new QueryWrapper<>();
        queryWrappermk.eq("skfuserid", bladeUser.getUserId());
        queryWrappermk.eq("moneytype", "1");
        queryWrappermk.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        List<MoneyDetail> list = moneyDetailService.list(queryWrappermk);
        BigDecimal sumMoney = new BigDecimal("0.00");
        for (MoneyDetail entity : list) {
            sumMoney = DecimalUtil.add(sumMoney, entity.getTrademoney());
        }
        OrderTjDTO result = new OrderTjDTO();
        result.setComplatecount(complateCount);
        result.setCanclecount(cancleCount);
        result.setMakemoney(sumMoney.toString());
        result.setTjtitle(sdf.format(date));
        return result;
    }

    ///前两月
    private OrderTjDTO getBeforTwoMonth(BladeUser bladeUser) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -2);
        Date date = cal.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        QueryWrapper<Order> queryWrappercm = new QueryWrapper<>();
        queryWrappercm.eq("workerid", bladeUser.getUserId());
        queryWrappercm.eq("orderstatus", "4");
        queryWrappercm.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        int complateCount = coalService.count(queryWrappercm);
        QueryWrapper<Order> queryWrappercn = new QueryWrapper<>();
        queryWrappercn.eq("workerid", bladeUser.getUserId());
        queryWrappercn.eq("orderstatus", "5");
        queryWrappercn.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        int cancleCount = coalService.count(queryWrappercn);


        QueryWrapper<MoneyDetail> queryWrappermk = new QueryWrapper<>();
        queryWrappermk.eq("skfuserid", bladeUser.getUserId());
        queryWrappermk.eq("moneytype", "1");
        queryWrappermk.apply("date_format(ctime,'%Y-%m')={0}", sdf.format(date));
        List<MoneyDetail> list = moneyDetailService.list(queryWrappermk);
        BigDecimal sumMoney = new BigDecimal("0.00");
        for (MoneyDetail entity : list) {
            sumMoney = DecimalUtil.add(sumMoney, entity.getTrademoney());
        }
        OrderTjDTO result = new OrderTjDTO();
        result.setComplatecount(complateCount);
        result.setCanclecount(cancleCount);
        result.setMakemoney(sumMoney.toString());
        result.setTjtitle(sdf.format(date));
        return result;
    }

    ////月份订单
    @GetMapping("/workderOrder3Month")
    public R workderOrder3Month(Order coal) {
        BladeUser bladeUser = AuthUtil.getUser();
        List<OrderTjDTO> result = new ArrayList<>();
        OrderTjDTO by = getThisMonth(bladeUser);
        OrderTjDTO sy = getBeforOneMonth(bladeUser);
        OrderTjDTO qy = getBeforTwoMonth(bladeUser);
        result.add(by);
        result.add(sy);
        result.add(qy);
        Integer value = 0;
        for (OrderTjDTO entity : result) {
            value = value + entity.getComplatecount();
        }
        return R.data(result);
    }

}
